package com.controller.information;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.artofsolving.jodconverter.DocumentConverter;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.po.DictionaryInfo;
import com.po.InformationInfo;
import com.po.MailBean;
import com.po.MyCollectionInfo;
import com.po.MyUploadInfo;
import com.po.ReczskInfo;
import com.po.SearchInfo;
import com.po.TagInfo;
import com.po.ThumbsUpInfo;
import com.po.UserZJInfo;
import com.service.configration.DictionaryService;
import com.service.configration.ReczskService;
import com.service.configration.TagService;
import com.service.infocenter.MyCollectionService;
import com.service.infocenter.MyUploadService;
import com.service.infocenter.RulePointService;
import com.service.information.InformationService;
import com.service.information.MailSenderService;
import com.service.information.ThumbsUpService;
import com.service.specialist.UserZJService;
import com.util.Change;
import com.util.ExportExcel;
import com.util.Office2PDF;
import com.util.StringFinal;
import com.util.UUIDGenerator;

@Controller
@RequestMapping("/jsp/information")
public class InformationController {
	@Resource(name = "informationServiceImp")
	private InformationService informationService;
	@Resource(name = "reczskServiceImp")
	private ReczskService reczskService;
	@Resource(name = "userZJServiceImp")
	private UserZJService userZJService;
	@Resource(name = "tagServiceImp")
	private TagService tagService;
	@Resource(name = "dictionaryServiceImp")
	private DictionaryService dictionaryService;
	@Resource(name = "myCollectionServiceImp")
	private MyCollectionService myCollectionService;
	@Resource(name = "thumbsUpServiceImp")
	private ThumbsUpService thumbsUpService;
	@Resource(name = "mailSenderServiceImp")
	private MailSenderService mailSenderService;
	@Resource(name = "rulePointServiceImp")
	private RulePointService rulePointService;
	@Resource(name="myUploadServiceImp")
	private MyUploadService myUploadService;	
	// 文件下载
	@ResponseBody
	@RequestMapping(value = "/p_sendemail")
	public int sendemail(HttpServletRequest request, @RequestParam("filename") String filename,
			@RequestParam("truename") String truename, @RequestParam("toemail") String toemail) throws Exception  {
		int result =1;
		// 获取文件的后缀
		filename = URLDecoder.decode(filename, "UTF-8");
		String[] names = filename.split("[.]");
		// suffix文件的后缀
		String suffix = names[names.length - 1];
		// 获取文件路径
		String path = request.getServletContext().getRealPath("") + filename;
		MailBean mailBean = new MailBean();
		mailBean.setFromName("陕西人才网");
		mailBean.setSubject("陕西人才发展知识库");
		mailBean.setToEmails(new String[] { toemail });
		mailBean.setContext("<html><body>您要下载的文件在附件中</body><html>");
		Map<String, String> attachments = new HashMap<String, String>();
		attachments.put(URLDecoder.decode(truename, "UTF-8") + "." + suffix, path);
		mailBean.setAttachments(attachments);
		try {
			mailSenderService.sendMail(mailBean);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			result=0;
		}
		return result;
	}

	    // 文件下载
		@RequestMapping(value = "/p_downloadn",produces="text/html;charset=UTF-8")
		public void downloadn(HttpServletRequest request, @RequestParam("filename") String filename,@RequestParam("infoid") String infoid,@RequestParam("status") String status,Model model,HttpServletResponse response) throws Exception {
				
			String path = request.getServletContext().getRealPath("");
			filename = URLDecoder.decode(filename, "UTF-8");
			System.out.println("finlename="+filename);
			File file = new File(path+File.separator+filename);
			if(file.exists()){
				//获取下载后文件显示的名字
				String[] names=filename.split("\\_", 2);	
				// 下载显示的文件名，解决中文名称乱码问题	
				String downloadFielName = new String((names[names.length-1]).getBytes("gbk"), "iso-8859-1");
				response.setCharacterEncoding("utf-8");
				response.setContentType("multipart/form-data");
				 response.setHeader("Content-Disposition", "attachment;fileName=" + downloadFielName);
				 try {
				        //打开本地文件流
				        InputStream inputStream = new FileInputStream(file);
				        //激活下载操作
				        OutputStream os = response.getOutputStream();

				        //循环写入输出流
				        byte[] b = new byte[2048];
				        int length;
				        while ((length = inputStream.read(b)) > 0) {
				            os.write(b, 0, length);
				        }
				        os.close();
				        inputStream.close();
						if(status.trim().equals("通过")){
							String userid = request.getSession().getAttribute("user_id").toString();
					         informationService.updateDownload(infoid,userid);
						}
				        
			   }
			  catch(Exception io){
				  request.getRequestDispatcher("../common/redirdown.jsp").forward(request, response);
				  return;
			  }

			}
			else{
				  request.getRequestDispatcher("../common/redirdown.jsp").forward(request, response);
				  
			}
				
		}
		
	@ResponseBody
	@RequestMapping(value = "/singleuploadInfo")
	public Object singleuploadInfo(@RequestParam("fileinput") MultipartFile file, InformationInfo info,
			HttpServletRequest request) {
	        info.setInfo_abstract(info.getInfo_abstract().replace(" ", "&nbsp").replace("\r\n", "<br>"));
		String uploaduser = request.getSession().getAttribute("user_id").toString();
		String uptime = new String(new SimpleDateFormat("yyyy-MM-dd  hh:mm:ss").format(new Date()));
		info.setInfo_id(UUIDGenerator.getUUID());
		info.setInfo_uptime(uptime);
		info.setUser_id(uploaduser);
		info.setInfo_status("未审核");
		info.setInfo_level(StringFinal.commlevel);
		String filename = file.getOriginalFilename();
		StringBuffer buffer = new StringBuffer();
		buffer.append(StringFinal.path).append(info.getInfo_id()).append("_").append(filename);
		try {
		String filepath = request.getSession().getServletContext().getRealPath(buffer.toString());
		info.setInfo_url(buffer.toString());
		File zsfile = new File(filepath);
			file.transferTo(zsfile);
		} catch (IllegalStateException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int result = informationService.addUploadinfo(info);
		return result;

	}

	
	// 通多ID获取单个知识的内容
	@ResponseBody
	@RequestMapping(value = "/getInformationByID")
	public InformationInfo getInformationByID(String info_id) {
		InformationInfo info = informationService.getOneInformation(info_id);
		return info;
	}

	// 库迁移
	@ResponseBody
	@RequestMapping(value = "/migrate")
	public int updateKu(String origin, String destination) {
		int i = informationService.updateInformationKu(origin, destination);
		return i;
	}

	// 库迁移
	@ResponseBody
	@RequestMapping(value = "/getkuInfoCount")
	public int getkuInfoCount(String origin) {
		int i = informationService.getInformationKuCount(origin);
		return i;
	}

	// 根据ID跳转到页面详情
	@RequestMapping(value = "/getInfoDetial")
	public String getInfoDital(Model model, HttpServletRequest request, HttpServletResponse resp, String info_id,String keyword)
			throws IOException {
		// 获取信息详情
		InformationInfo info = informationService.getOneInformation(info_id);
		model.addAttribute("info", info);

		informationService.updateBrowser(info_id);
		/*List<InformationInfo> recomlist = informationService.getWbrecommendList(keyword,info_id);
		model.addAttribute("recomlist", recomlist);*/
		// 获取文件中存储的文件名称
		String download_name = info.getInfo_url();
		String[] names = download_name.split("\\_", 2);
		
		model.addAttribute("download_name", names[names.length-1]);
		// 获取该条知识的收藏数
		MyCollectionInfo myCollectionInfo = new MyCollectionInfo();
		myCollectionInfo.setId(info_id);
		int goodcount = myCollectionService.getCollectionCountByID(myCollectionInfo);
		model.addAttribute("goodcount", goodcount);
		// 获取该条知识的点赞数
		ThumbsUpInfo thumbsupinfo = new ThumbsUpInfo();
		thumbsupinfo.setInfo_id(info_id);
		int thumbsupcount = thumbsUpService.getThumbsUpCount(thumbsupinfo);
		model.addAttribute("thumbsupcount", thumbsupcount);
		// 获取专家列表
		UserZJInfo userzjinfo = new UserZJInfo();
		userzjinfo.setPage(0);
		userzjinfo.setRows(6);
		List<UserZJInfo> listzj = userZJService.getUserZJList(userzjinfo);
		model.addAttribute("listzj", listzj);
		// 显示下载知识所需积分
		int downpoint=rulePointService.getRulePointNumber("每个用户初次下载知识扣除积分");
		model.addAttribute("downpoint", -downpoint);
		int uppoint=rulePointService.getRulePointNumber("每上传一条知识获取积分数");
		model.addAttribute("uppoint", uppoint);
		// 显示该用户时候已经收藏了这条知识
		if (request.getSession().getAttribute("user_id") != null) {
			String uploaduser = request.getSession().getAttribute("user_id").toString();
			myCollectionInfo.setUser_id(uploaduser);
			int mycollect = myCollectionService.getCollectionCountByID(myCollectionInfo);
			if (mycollect != 0) {
				model.addAttribute("collectimage", "../../image/icon/star-full.png");
			} else {
				model.addAttribute("collectimage", "../../image/icon/star-empty.png");
			}
		} else
			model.addAttribute("collectimage", "../../image/icon/star-empty.png");

		// 显示该用户时候已经点赞了这条知识
		if (request.getSession().getAttribute("user_id") != null) {
			String user = request.getSession().getAttribute("user_id").toString();
			thumbsupinfo.setUser_id(user);
			int thumbsup = thumbsUpService.getThumbsUpCount(thumbsupinfo);
			if (thumbsup != 0) {
				model.addAttribute("thumbsupimage", "../../image/icon/like-red.png");
			} else {
				model.addAttribute("thumbsupimage", "../../image/icon/like-black.png");
			}
		} else
			model.addAttribute("thumbsupimage", "../../image/icon/like-black.png");
		return "information/infodetail";
	}


	// 首页点击检索按钮跳转至检索页面，只是传递检索信息进入检索页面
	@RequestMapping(value = "/getSearchInfo")
	public String getSearchInfo(Model model, HttpServletRequest request, HttpServletResponse resp, String category,
			String content, String reczsk_id) throws IOException {
		model.addAttribute("category", category);
		if(content !=null)
		model.addAttribute("content", URLDecoder.decode(content, "UTF-8"));
		model.addAttribute("reczsk_id", reczsk_id);
		return "information/infosearch";
	}

	// 首页点击检索按钮跳转至高级检索页面
	@ResponseBody
	@RequestMapping(value = "/getADSearchInformation")
	public Map<String, Object> getADSearchInformation(int limit, int offset, String jstring, String reczsk_id,
			String tag_name, String time) {
		List<SearchInfo> searchlist = Change.json_to_SearchInfo(jstring);
		String[] reczsk_ids = {};
		if (reczsk_id != "" && reczsk_id != null) {
			reczsk_ids = reczsk_id.split(",");
		}
		List<SearchInfo> orlist = searchlist.subList(0, 3);
		for (int i = 0; i < orlist.size() - 1; i++) {
			if (orlist.get(i).getContent().equals("") && orlist.get(i + 1).getAndor().equals("or")) {
				orlist.get(i + 1).setAndor("and");
			}
		}
		List<SearchInfo> andlist = searchlist.subList(3, 9);
		List<InformationInfo> list = informationService.getADSearchList(offset, limit, orlist, andlist, reczsk_ids,
				tag_name, time);
		int count = informationService.getADSearchCount(offset, limit, orlist, andlist, reczsk_ids, tag_name, time);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}

	// 首页点击检索按钮跳转至检索页面
	@ResponseBody
	@RequestMapping(value = "/getSearchInformation")
	public Map<String, Object> getSearchInformation(int limit, int offset, String category, String content,
			String reczsk_id, String tag_name, String time) {
		String[] reczsk_ids = {};
		if (reczsk_id != "" && reczsk_id != null) {
			reczsk_ids = reczsk_id.split(",");
		}
		List<InformationInfo> infolist = informationService.getSearchList(offset, limit, category, content, reczsk_ids,
				tag_name, time);
		int count = informationService.getSearchCount(category, content, reczsk_ids, tag_name, time);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", infolist);
		return map;
	}

	// 首页展示
	@RequestMapping(value = "/getInformation")
	public String getInformation(Model model, HttpServletRequest request, HttpServletResponse resp) throws IOException {	   
	    String guojiakuid=reczskService.getReczskByName("国家人才政策");
	    InformationInfo info1 = new InformationInfo();
		info1.setInfo_status("通过");
		info1.setPage(0);
		info1.setRows(12);
		info1.setFirst_class(guojiakuid);
		List<InformationInfo> list1 = informationService.getInformationList(info1);
		model.addAttribute("gjlist", list1);
		
		String shanxikuid=reczskService.getReczskByName("陕西省人才政策");
	    InformationInfo info2 = new InformationInfo();
		info2.setInfo_status("通过");
		info2.setPage(0);
		info2.setRows(12);
		info2.setFirst_class(shanxikuid);
		List<InformationInfo> list2 = informationService.getInformationList(info2);
		model.addAttribute("sxlist", list2);

		List<InformationInfo> recomlist = informationService.getRecommendList();
		model.addAttribute("recomlist", recomlist);
		
		UserZJInfo userzjinfo = new UserZJInfo();
		userzjinfo.setPage(0);
		userzjinfo.setRows(4);
		List<UserZJInfo> listzj = userZJService.getUserZJList(userzjinfo);
		model.addAttribute("listzj", listzj);

		ReczskInfo reczsk = new ReczskInfo();
		reczsk.setLevel(1);
		List<ReczskInfo> listt = reczskService.getReczskList(reczsk);
		model.addAttribute("fuku", listt);
		int i = 0;
		for (ReczskInfo reczskInfo : listt) {
			reczsk.setpId(reczskInfo.getId());
			reczsk.setLevel(reczskInfo.getLevel() + 1);
			List<ReczskInfo> listz = reczskService.getReczskList(reczsk);
			listt.get(i).setList(listz);
			i++;
		}
		 informationService.updateAccess();
		return "infocenter/index";
	}

	
	@RequestMapping(value = "/getinfosearchview")
	public String getinfoSearchview() {

		return "information/infosearch";
	}

	// 检索未审核知识
	@ResponseBody
	@RequestMapping(value = "/getCheckInfo")
	public Map<String, Object> getUpload(int limit, int offset, String departmentname, String statu) {
		InformationInfo info = new InformationInfo();
		info.setInfo_name(departmentname);
		info.setInfo_status("未审核");
		info.setPage(offset);
		info.setRows(limit);
		List<InformationInfo> list = informationService.getInformationList(info);
		int count = informationService.getInformationCount(info);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}

	// 查询时更新页面的标签,其他页面的标签修改
	// 根据库ID获取标签以及标签中的内容
	// 参数可以是多个ID的字符串
	@ResponseBody
	@RequestMapping(value = "/updateSearchTag")
	public List<TagInfo> updateSearchTag(String reczsk_id) {
		List<TagInfo> taglist = tagService.getTagByIDS(reczsk_id);
		return taglist;
	}

	// 查询时更新页面的标签,搜索页面的标签修改
		// 根据库ID获取标签以及标签中的内容
		// 参数可以是多个ID的字符串
		@ResponseBody
		@RequestMapping(value = "/updateforSearchTag")
		public List<TagInfo> updateforSearchTag(String category, String content,
				String reczsk_id, String tag_name, String time) {
			//List<TagInfo> taglist = tagService.getTagByIDS(reczsk_id);
			List<TagInfo> taglist = informationService.getSearchTagList(category, content, reczsk_id, tag_name, time);
			List<DictionaryInfo> yearlist = informationService.getSearchYearList(category, content, reczsk_id, tag_name, time);
			TagInfo tag = new TagInfo();
			tag.setTag_id("fbnf");
			tag.setName("发表年份");
			tag.setList(yearlist);
			taglist.add(tag);
			return taglist;
		}

	// 查询时更新页面的标签
	// 根据库ID获取标签以及标签中的内容
	// 参数可以是多个ID的字符串
	@ResponseBody
	@RequestMapping(value = "/updateAdSearchTag")
	public List<TagInfo> updateAdSearchTag(String jstring, String reczsk_id,
			String tag_name, String time) {
		//List<TagInfo> taglist = tagService.getTagByIDS(reczsk_id);
		List<SearchInfo> searchlist = Change.json_to_SearchInfo(jstring);
		List<SearchInfo> orlist = searchlist.subList(0, 3);
		for (int i = 0; i < orlist.size() - 1; i++) {
			if (orlist.get(i).getContent().equals("") && orlist.get(i + 1).getAndor().equals("or")) {
				orlist.get(i + 1).setAndor("and");
			}
		}
		List<SearchInfo> andlist = searchlist.subList(3, 9);
		List<TagInfo> taglist = informationService.getADSearchTagList(orlist, andlist, reczsk_id, tag_name, time);
		List<DictionaryInfo> yearlist = informationService.getADSearchYearList(orlist, andlist, reczsk_id, tag_name, time);
		TagInfo tag = new TagInfo();
		tag.setTag_id("adfbnf");
		tag.setName("发表年份");
		tag.setList(yearlist);
		taglist.add(tag);
		return taglist;
	}
	@RequestMapping(value = "/p_getuploadview")
	public String getUploadview() {

		return "information/infoupload";
	}
	/**
	 * 知识迁移视图
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getMigrateview")
	public String getMigrateview() {

		return "infocenter/migrate";
	}
	/**
	 * 返回未审核的列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getExamineview")
	public String getExamineview() {

		return "infocenter/examine";
	}

	/**
	 * 返回未审核的详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getExamineCheckview")
	public String getExamineCheckview(Model model, HttpServletRequest request, HttpServletResponse resp,
			String info_id) {
		model.addAttribute("id", info_id);
		return "infocenter/infocheck";
	}
	
	@ResponseBody
	@RequestMapping(value="/updatefileCheckInfo")
	public int  getfileCheckInfo(@RequestParam("fileinput")MultipartFile file,InformationInfo info,HttpServletRequest request){
		info.setInfo_abstract(info.getInfo_abstract().replace(" ", "&nbsp").replace("\r\n", "<br>"));
	        int result =0;
		//原来文件的路径
		String filepath = request.getServletContext().getRealPath(info.getInfo_url());
		//新文件的路径
		StringBuffer newfilepath = new StringBuffer(StringFinal.path);
		newfilepath.append(info.getInfo_id()).append("_").append(file.getOriginalFilename());
		info.setInfo_url(newfilepath.toString());
		info.setInfo_status("通过");
			File originfile = new File(filepath);
			if(originfile.exists())
				{
					originfile.delete();
					
				}
			String newpath = request.getSession().getServletContext().getRealPath(newfilepath.toString());
			File newfile = new File(newpath);
			try {
				
				file.transferTo(newfile);
				result = informationService.updateInformation(info);
				//用于文件预览
				String pf = info.getInfo_url().split("\\.")[1];
				if(pf.contains("doc")||pf.contains("ppt")||pf.contains("xls"))
				{
					DocumentConverter converter =  Office2PDF.startService();
					StringBuffer viewbuffer = new StringBuffer();
					String viewname =  viewbuffer.append(info.getInfo_url().split("/")[1].split("\\.")[0]).append(StringFinal.postfix).toString();
					String viewpath = request.getServletContext().getRealPath("/"+StringFinal.viewpath+viewname);
					File viewfile = new File(viewpath);
					converter.convert(newfile, viewfile);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				result=0;
				e.printStackTrace();
			}
			finally{
				Office2PDF.stopService();
			}
		return result;	
	}
	@ResponseBody
	@RequestMapping(value="/updateCheckInfo")
	public int  getnofileCheckInfo(InformationInfo info,HttpServletRequest request){
	        info.setInfo_abstract(info.getInfo_abstract().replace(" ", "&nbsp").replace("\r\n", "<br>"));        
		info.setInfo_status("通过");
		int result = informationService.updateInformation(info);
		try{
			//用于文件预览
			String pf = info.getInfo_url().split("\\.")[1];
			if(pf.contains("doc")||pf.contains("ppt")||pf.contains("xls"))
			{
				DocumentConverter converter =  Office2PDF.startService();
				StringBuffer viewbuffer = new StringBuffer();
				String viewname =  viewbuffer.append(info.getInfo_url().split("/")[1].split("\\.")[0]).append(StringFinal.postfix).toString();
				String viewpath = request.getServletContext().getRealPath("/"+StringFinal.viewpath+viewname);
				File originfile = new File(request.getServletContext().getRealPath("/"+info.getInfo_url()));
				File viewfile = new File(viewpath);
				converter.convert(originfile, viewfile);
			}
		}
		catch(Exception e){
			e.printStackTrace();
			result = 0;
		}
		finally{
			Office2PDF.stopService();
		}
		return result;
	}
	@ResponseBody
	@RequestMapping(value="/delCheckInformation")
	public int delCheckInformation(InformationInfo info,HttpServletRequest request){
		
		int result = informationService.deleteInformations(info);
		if(result>0){
			String filepath = request.getServletContext().getRealPath(info.getInfo_url());
			File file = new File(filepath);
			if(file.exists()){
				file.delete();
			}
		}
		return result;
	}
	/**
	 * 知识管理 删除知识
	 * @param myUploadInfo
	 * @param zt
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteUpload")
	public int deleteUpload(MyUploadInfo myUploadInfo,String zt,HttpServletRequest request){
		int i=myUploadService.deleteMyUpload(myUploadInfo);
		//zt用来判断是从我的上传还是知识管理传过来的请求
		String userid = request.getSession().getAttribute("user_id").toString();
		myUploadInfo.setUser_id(userid);
		myUploadService.addInfodelete(myUploadInfo);
		if(i>0){
			StringBuffer buffer = new StringBuffer(request.getServletContext().getRealPath(myUploadInfo.getInfourl()));
			File delfile = new File(buffer.toString());
			
			if(delfile.exists()){
				delfile.delete();
				//用于问件预览
				String pf = myUploadInfo.getInfourl().split("\\.")[1];
				if(pf.contains("doc")||pf.contains("ppt")||pf.contains("xls"))
				{
					StringBuffer viewbuffer = new StringBuffer(request.getServletContext().getRealPath(StringFinal.viewpath+myUploadInfo.getInfourl().split("/")[1].split("\\.")[0]+".pdf"));
					File delviewfile = new File(viewbuffer.toString());
					if(delviewfile.exists())
						delviewfile.delete();
					viewbuffer.setLength(0);
				}
				
			}
			else{
				i=0;
			}	
		}
		return i;
	}
	/**
	 * 知识管理 批量删除知识
	 * @param ids
	 * @param urls
	 * @param names
	 * @param zt
	 * @param kutypes
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/deleteUploads")
	public int deleteUploads(String ids,@RequestParam("urls")String[] urls,@RequestParam("names")String[] names,String zt,@RequestParam("kutypes")String[] kutypes,HttpServletRequest request){
		String[] id = ids.split(",");
		int i=myUploadService.deleteMyUploads(id,urls,request.getServletContext().getRealPath(""));
		String userid = request.getSession().getAttribute("user_id").toString();
		myUploadService.addInfodeleteList(id, urls, names, kutypes, userid);
		return i;
	}
	
	/**
	 * 返回知识修改
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getUpdateview")
	public String getInfoupdate(Model model, HttpServletRequest request, HttpServletResponse resp,
			String info_id) {
		String str ="infocenter/infoupdate";
		model.addAttribute("id", info_id);
		return str;
	}
	/**
	 * 返回知识修改知识管理员
	 * 
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	@RequestMapping(value = "/getUpdatemview")
	public String getInfomupdate(Model model, HttpServletRequest request, HttpServletResponse resp,
			 String info) throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		InformationInfo infomation = mapper.readValue(info,InformationInfo.class);
		model.addAttribute("info",infomation);
		return "infocenter/infomupdate";
	}
		/**
	 * 修改知识
	 * @param file
	 * @param info
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updatefileInfo")
	public int  updatefileInfo(@RequestParam("fileinput")MultipartFile file,InformationInfo info,HttpServletRequest request){
		int result =0;
		//原来文件的路径
		String filepath = request.getServletContext().getRealPath(info.getInfo_url());
		//新文件的路径
		StringBuffer newfilepath = new StringBuffer(StringFinal.path);
		newfilepath.append(info.getInfo_id()).append("_").append(file.getOriginalFilename());
		info.setInfo_url(newfilepath.toString());
		info.setInfo_status("未审核");
		result = informationService.updateInformation(info);
		if(result>0){
			File originfile = new File(filepath);
			if(originfile.exists())
				{
					originfile.delete();
					
				}
			String newpath = request.getSession().getServletContext().getRealPath(newfilepath.toString());
			File newfile = new File(newpath);
			try {
				
				file.transferTo(newfile);
				result =1;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				result=0;
				e.printStackTrace();
			}
				
		}
		return result;
	}
	/**
	 * 修改知识
	 * @param info
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateInfo")
	public int  updateInfo(InformationInfo info){
		info.setInfo_status("未审核");
		int result = informationService.updateInformation(info);
		return result;
	}
	
	/**
	 * 返回知识管理
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getInfoManageview")
	public String getInfomanage(Model model, HttpServletRequest request, HttpServletResponse resp,
			String info_id) {
		model.addAttribute("id", info_id);
		return "infocenter/infomanage";
	}
	
	/**
	 * 返回信息list
	 * @param infoname
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getInfomanage")
	public  Map<String, Object> getInfoList(String infoname,String kudata,String level,int limit, int offset){
		
		List<InformationInfo> list = informationService.getInfoManage(infoname,kudata,level,limit,offset);
		int count = informationService.getInfoManageCount(infoname,kudata,level);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", count);
		map.put("rows", list);
		return map;
	}
	/**
	 * 单个设置信息是否是vip
	 * @param level
	 * @param infoid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateInfolevel")
	public int updateInfolevel(String level,String infoid){
		int result = informationService.updateInfolevel(level,infoid);
		return result;
	}
	
	/**
	 * 批量设置信息是否是vip
	 * @param level
	 * @param infoid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateInfoslevel")
	public int updateInfoslevel(String level,String ids){
		int result = informationService.updateInfoslevel(level,ids);
		return result;
	}
	
	/**
	 * 判断是否允许下载
	 * @param downtype
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPermitload")
	public int getRulePoint(String userid){
		
		int mypoint = rulePointService.getPersonalPoint(userid);
		int downloadpoint = rulePointService.getRulePointNumber("每个用户初次下载知识扣除积分");
		if(mypoint+downloadpoint>=0)
		  return 1;
		else
		  return 0;
		}
	
	/**
	 * 批量迁移信息
	 * @param level
	 * @param infoid
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/updateInfosclass")
	public int updateInfosclass(String ids,String kudata){
		int result = informationService.updateInfosclass(ids,kudata);
		return result;
	}
	/**
	 * 导出excel信息
	 * @param infoname
	 * @param kudata
	 * @param level
	 * @param response
	 */
	@RequestMapping(value="/getExcelFile")
	public void getInfoExcelFile(String infoname,String kudata,String level,HttpServletResponse response){
		try {
			String inforname = URLDecoder.decode(infoname, "UTF-8");
			List<InformationInfo> list = informationService.getInfoExcel(inforname, kudata, level);
			ExportExcel export = new ExportExcel();
			export.exportExcel(list, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/getRecommendlist")
	public List<InformationInfo> getRecommendList(Model model, HttpServletRequest request, String info_id,String keyword){
		
		List<InformationInfo> list = informationService.getWbrecommendList(keyword, info_id);
		return list;
	}
	/**
	 * 判断是否允许下载
	 * @param downtype
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getPersonpoint")
	public int getPersonpoint(String userid){
		
		int mypoint = rulePointService.getPersonalPoint(userid);
		
		  return mypoint;
		}

}